# -*- coding: UTF-8 -*-
# @description : python
# @Author : zyl
# @Time : 2021/8/27 16:27
# @Remark: 视频监控配置视图
from mxsoftpy import View, Response
from server.common.VideoManage.VideoMonitor.MonitorConfig import MonitorConfigServer


class MonitorConfigAdd(View):

    def post(self):
        """
        添加配置
        """
        try:
            return '新增成功', {'id': MonitorConfigServer().create(**self.request.POST)}
        except Exception as e:
            return str(e)


class MonitorConfigList(View):

    def get(self):
        """
        获取列表
        """
        callback = self.request.callback
        if callback:
            data = '%s(%s)' % (callback, MonitorConfigServer().list(**self.request.GET))
        else:
            data = MonitorConfigServer().list(**self.request.GET)
        return Response(data)


class MonitorConfigUpdate(View):

    def post(self):
        """
        更新配置
        """
        try:
            return '修改成功', MonitorConfigServer().update(**self.request.POST)
        except Exception as e:
            return str(e)


class MonitorConfigDelete(View):

    def get(self):
        """
        删除配置
        """

        return '删除成功', MonitorConfigServer().delete(**self.request.GET)


class MonitorConfigRetrieve(View):

    def get(self):
        """
        回显数据
        """

        return '', MonitorConfigServer().retrieve(**self.request.GET)


class MonitorConfigMove(View):

    def get(self):
        """
        批量移动
        :return:
        """
        return '', MonitorConfigServer().move(**self.request.GET)


class MonitorConfigSort(View):

    def post(self):
        """
        批量排序
        :return:
        """
        return '排序成功', MonitorConfigServer().sort(**self.request.POST)


class MonitorConfigDownload(View):

    def get(self):
        """
        下载模板
        :return:
        """
        sio = MonitorConfigServer().download()
        self.request.add_header('Content-Transfer-Encoding', 'binary')
        self.request.add_header('Content-Type', 'application/force-download')
        self.request.add_header('Content-Type', 'application/octet-stream')
        self.request.add_header('Content-Type', 'application/download')
        self.request.add_header('Content-Disposition', 'inline;filename=' + 'monitorConf.xls')
        self.request.add_header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0')
        self.request.add_header('Pragma', 'no-cache')
        return Response(sio)


class MonitorConfigImport(View):

    def post(self):
        """
        批量导入
        :return:
        """
        # try:
        path = self.request.web_path + r'/hdoc/tmp/'
        print(path)
        res = self.request.session.UploadFile(path, True)
        print(res)
        if res[0] is False:
            return '上传文件出现错误'
        if path[-1] == '/':
            path += res[1]
        else:
            path += '/' + res[1]
        sbool, error = MonitorConfigServer().ConfigImport(path)
        if sbool:
            return '导入成功', ''
        else:
            return error
        # except Exception as e:
        #     return '数据导入失败'
